package com.chixing.service.impl;

import com.chixing.mapper.ShopMapper;
import com.chixing.pojo.Shop;
import com.chixing.pojo.ShopExample;
import com.chixing.pojo.ShopVO;
import com.chixing.service.BranchService;
import com.chixing.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BranchServiceImpl implements BranchService {
    @Autowired
    private ShopMapper shopMapper;

    @Override
    public ServerResult getAllShop() {

        List<Shop> shopList = shopMapper.selectByExample(null);
        System.out.println(shopList);
        ShopVO shopVO = new ShopVO();
        shopVO.setShopList(shopList);
        if (shopVO != null)
            return ServerResult.getSussess(shopVO);
        return ServerResult.getFail(null);
    }

    @Override
    public Shop getByNameCity(String shopName, String city) {

        ShopExample example = new ShopExample();
        example.createCriteria().andShopNameLike("%" + shopName + "%").andShopAddressLike("%" + city + "%");
        List<Shop> shopList = shopMapper.selectByExample(example);

        if (shopList != null && shopList.size() > 0)
            return shopList.get(0);
        return null;
    }

    @Override
    public String getAddById(Integer shopId) {

        String address = shopMapper.selectByPrimaryKey(shopId).getShopAddress();

        return address;
    }

    @Override
    public ServerResult getByShopId(Integer shopId) {
        Shop shop = shopMapper.selectByPrimaryKey (shopId);
        if (shop!=null){
            return ServerResult.getSussess (shop);
        }
        return ServerResult.getFail (null);
    }
}
